<div class="advance_box">
    <!-- 高级检索 -->
    <div class="advanceSearch">
        <p class="one">
            <select class="selectTwo">
                <option value='题名' selected>题名</option>
                <option  value='主题'>主题</option>
                <option value='作者'>作者</option>
                <option value='关键词'>关键词</option>
            </select>
            <input type="text" class="">
            <!-- 添加，减少按钮 -->
            <button id='add' class="addBtn"></button>
        </p>
        <div id='searchPart' class="content">
            <p class="two">
                <select class="selectOne">
                    <option selected>并且</option>
                    <option>或者</option>
                </select>
                <select class="selectTwo">
                    <option value='题名' selected>题名</option>
                    <option  value='主题'>主题</option>
                    <option value='作者'>作者</option>
                    <option value='关键词'>关键词</option>
                </select>
                <input type="text" class='text'>
                <button class='increase'></button>
            </p>
            <p class="two">
                <select class="selectOne">
                    <option selected>并且</option>
                    <option>或者</option>
                </select>
                <select class="selectTwo">
                    <option value='题名' selected>题名</option>
                    <option value='主题'>主题</option>
                    <option value='作者'>作者</option>
                    <option value='关键词'>关键词</option>
                </select>
                <input type="text" class='text'>
                <button class='increase'></button>
            </p>
        </div>
        <!-- 选择日期 -->
        <div class="date">
            <span>发布时间:</span>      从      <input id="year" type="text" value="" /><span>至</span><input id="toDate" type="text"
                value="" />
            <!-- 更新时间 -->
            <div class="updateDate"><span>创建时间:</span>
                <select name="" id='createDate'>
                    <option value="" selected>不限</option>
                    <option value="最近一周">最近一周</option>
                    <option value="最近一月">最近一月</option>
                    <option value="最近半年">最近半年</option>
                    <option value="最近一年">最近一年</option>
                </select>
            </div>
            <!-- 图标 -->
            <i class="dateIcon"></i>
            <i class="dateIcon icon2"></i>
        </div>
        <!-- 检索 -->
        <div class="searchBtn">
            <span id='preSearch' class="firstStyle"></span>
            <button class="borderNone search" id="firstSearch" >检索</button>
                <button class="borderNone" id="searchResult">在结果中检索</button>
            <!-- <input type="checkbox" id="searchResult"><label for="searchResult"></label> -->
        </div>
    </div>
</div>
<script>
    function getAndVal() {
        var result = '';
        var first = base.replaceWord($('.advanceSearch>.one>input').val());
        // 如果第一项有值
        if (first) {
            if ($('.advanceSearch>.one>select').find("option:selected").val() == '主题') {
                var firstText = '(' + $('.advanceSearch>.one>select').find("option:selected").val() + " %=" + '"' + first + '"';
            } else if ($('.advanceSearch>.one>select').find("option:selected").val() == '题名') {
                var firstText = '(' + $('.advanceSearch>.one>select').find("option:selected").val() + " %=" + '"' + first + '"';
            } else if ($('.advanceSearch>.one>select').find("option:selected").val() == '作者') {
                var firstText = '(' + $('.advanceSearch>.one>select').find("option:selected").val() + " %=" + '"' + first + '"';
            } else if ($('.advanceSearch>.one>select').find("option:selected").val() == '关键词') {
                var firstText = '(' + $('.advanceSearch>.one>select').find("option:selected").val() + " %=" + '"' + first + '"';
            } else if ($('.advanceSearch>.one>select').find("option:selected").val() == '文献来源') {
                var firstText = '(' + $('.advanceSearch>.one>select').find("option:selected").val() + " %=" + '"' + first + '"';
            } else {
                var firstText = '(' + $('.advanceSearch>.one>select').find("option:selected").val() + " =" + '"*' + first + '"';
            }

            result += firstText;
            for (var i = 0; i < $('.advanceSearch .two').length; i++) {
                // 获取选择作者、主题等值
                var title = $($('.advanceSearch .two>.selectTwo')[i]).val();
                // 获取文本框的值
                var text = base.replaceWord($($('.advanceSearch .two>.text')[i]).val());
                //  如果文本框有值
                if (text) {
                    // 选择并且、或者
                    if ($($('.advanceSearch .two>.selectOne')[i]).val() == '或者') {
                        if (title == '主题') {
                            result += ')' + ' or ' + '(' + title + " %=" + '"' + text + '"';
                        } else if (title == '题名') {
                            result += ')' + ' or ' + '(' + title + " %=" + '"' + text + '"';
                        } else if (title == '作者') {
                            result += ')' + ' or ' + '(' + title + " %=" + '"' + text + '"';
                        } else if (title == '关键词') {
                            result += ')' + ' or ' + '(' + title + " %=" + '"' + text + '"';
                        } else if (title == '文献来源') {
                            result += ')' + ' or ' + '(' + title + " %=" + '"' + text + '"';
                        } else {
                            result += ')' + ' or ' + '(' + title + " =" + '"*' + text + '"';
                        }
                    } else {
                        if (title == '主题') {
                            result += ' and ' + title + " %=" + '"' + text + '"';
                        } else if (title == '题名') {
                            result += ' and ' + title + " %=" + '"' + text + '"';
                        } else if (title == '作者') {
                            result += ' and ' + title + " %=" + '"' + text + '"';
                        } else if (title == '关键词') {
                            result += ' and ' + title + " %=" + '"' + text + '"';
                        } else if (title == '文献来源') {
                            result += ' and ' + title + " %=" + '"' + text + '"';
                        } else {
                            result += ' and ' + title + " =" + '"*' + text + '"';
                        }

                    }
                } else {
                    //没有值
                    result += '';
                }
            }
        }
        else {
            //如果第一项没有值
            for (var i = 0; i < $('.advanceSearch .two').length; i++) {
                // 获取选择作者、主题等值
                var title = $($('.advanceSearch .two>.selectTwo')[i]).val();
                // 获取文本框的值
                var text = base.replaceWord($($('.advanceSearch .two>.text')[i]).val());
                //  如果文本框有值  
                if (text) {
                    var j = i;
                    // 选择并且、或者
                    // 当前项
                    if (title == '主题') {
                        var firstText = '(' + title + " %=" + '"' + text + '"';
                    } else if (title == '题名') {
                        var firstText = '(' + title + " %=" + '"' + text + '"';
                    } else if (title == '作者') {
                        var firstText = '(' + title + " %=" + '"' + text + '"';
                    } else if (title == '关键词') {
                        var firstText = '(' + title + " %=" + '"' + text + '"';
                    }else if (title == '文献来源') {
                        var firstText = '(' + title + " %=" + '"' + text + '"';
                    }else {
                        var firstText = '(' + title + " =" + '"*' + text + '"';
                    }
                    result = firstText;
                    break;
                }
            }
            // 取出该值，重新循环
            for (var j = j + 1; j < $('.advanceSearch .two').length; j++) {
                // 获取选择作者、主题等值
                var title = $($('.advanceSearch .two>.selectTwo')[j]).val();
                // 获取文本框的值
                var text = base.replaceWord($($('.advanceSearch .two>.text')[j]).val());
                //  如果文本框有值
                if (text) {
                    // 选择并且、或者
                    if ($($('.advanceSearch .two>.selectOne')[j]).val() == '或者') {
                        if (title == '主题') {
                            result += ')' + ' or ' + '(' + title + " %=" + '"' + text + '"';
                        } else if (title == '题名') {
                            result += ')' + ' or ' + '(' + title + " %=" + '"' + text + '"';
                        } else if (title == '作者') {
                            result += ')' + ' or ' + '(' + title + " %=" + '"' + text + '"';
                        } else if (title == '关键词') {
                            result += ')' + ' or ' + '(' + title + " %=" + '"' + text + '"';
                        }else if (title == '文献来源') {
                            result += ')' + ' or ' + '(' + title + " %=" + '"' + text + '"';
                        }else {
                            result += ')' + ' or ' + '(' + title + " =" + '"*' + text + '"';
                        }

                    } else {
                        if (title == '主题') {
                            result += ' and ' + title + " %=" + '"' + text + '"';
                        } else if (title == '题名') {
                            result += ')' + ' or ' + '(' + title + " %=" + '"' + text + '"';
                        } else if (title == '作者') {
                            result += ')' + ' or ' + '(' + title + " %=" + '"' + text + '"';
                        }else if (title == '关键词') {
                            result += ')' + ' or ' + '(' + title + " %=" + '"' + text + '"';
                        }else if (title == '文献来源') {
                            result += ')' + ' or ' + '(' + title + " %=" + '"' + text + '"';
                        } else {
                            result += ' and ' + title + " =" + '"*' + text + '"';
                        }
                    }
                } else {
                    //没有值
                    result += '';
                }
            }
        }

        result += ')';
        //防止传")"
        if (result == ')') {
            result = '';
        }
        return result;
    }
    $(function () {
        //执行一个laydate实例
        var startDate = laydate.render({
            elem: '#year',
            theme: '#39b2f7',
            done: function (value, date) {
                endDate.config.min = {
                    year: date.year,
                    month: date.month - 1,
                    date: date.date,
                };
            }
        });
        var endDate = laydate.render({
            elem: '#toDate',
            theme: '#39b2f7',
            done: function (value, date) {
                startDate.config.max = {
                    year: date.year,
                    month: date.month - 1,
                    date: date.date,
                }
            }
        })
        // 删除按钮
        $('#searchPart').click(function (e) {
            if($(e.target).hasClass('increase')){
                $(e.target).parent('p').remove();
                showColor();
            }
            // $(".content>.two").last().remove();
        })
        //  增加按钮
        $('#add').click(function () {
            if ($('.advanceSearch .two').length < 4) {
                // 获取最后一个下拉框的样式。判断值是并且还是或者
                var addColor = $($('.selectTwo').eq($('.advanceSearch .two').length)[0])[0].className.split(' ')[1]
                var Val = $('.selectOne').eq($('.advanceSearch .two').length - 1).val();
                if (addColor) {
                    $('#searchPart').append('<p class="two"><select class="selectOne ' + addColor + '"><option selected>并且</option><option>或者</option></select><select class="selectTwo ' + addColor + '"><option value="题名" selected>题名</option><option  value="主题">主题</option><option value="作者">作者</option><option value="关键词">关键词</option></select><input type="text" class="text ' + addColor + '"><button class="increase"></button></p>')
                } else {
                    //没有颜色
                    $('#searchPart').append('<p class="two"><select class="selectOne"><option selected>并且</option><option>或者</option></select><select class="selectTwo"><option value="题名" selected>题名</option><option  value="主题">主题</option><option value="作者">作者</option><option value="关键词">关键词</option></select><input type="text" class="text"><button class="increase"></button></p>')
                }


            }
            showColor();
        })

        function showColor() {
            //切换颜色样式
            //切换颜色样式
            var colorList = ["#a9d7f3", "#f2b0b0", "#fad1af", "rgb(178, 245, 223)", "cadetblue"];
            var colorIndex = 0;
            var list = [colorIndex]
            $('.advanceSearch .selectOne').each(function (index, item) {
                $(item).val() == "并且" ? list.push(colorIndex) : list.push(++colorIndex);
                $(item).change(showColor)
            })

            $('.advanceSearch .selectTwo').each(function (index, item) {
                var color = colorList[list[index]]
                item.style.backgroundColor = color
                $(item).siblings(".selectOne").css("backgroundColor", color)
                $(item).siblings("input").css("backgroundColor", color)
            })
        }
        showColor();

    })
</script>